import NetService from './net.service'
import { ref, reactive } from 'vue'
import { Service, injectable, mutex } from './service'

@injectable
export default class PlatformService extends Service {
  public platforms = ref([] as Any[])

  public addData = reactive({
    id: '',
    station: '',
    direction: '',
    section1: '',
    section2: '',
    sort: null
    // turnout: '',
    // stop: null,
    // signal: null
  })

  private netService = new NetService()

  @mutex
  async load () {
    // 加载全部 区段 数据
    // 判断是否全部加载 如果已经加载 忽略
    const res = await this.netService.get('/platforms/list')
    if (res?.success) {
      res?.data.forEach((item: { id: { toString: (arg0: number) => string }; station: { toString: (arg0: number) => string }; section1: { toString: (arg0: number) => string }; section2: { toString: (arg0: number) => string } }, index: number) => {
        item.id = item.id.toString(16).replace('"', '').replace('"', '').toUpperCase()
        item.station = item.station?.toString(16).replace('"', '').replace('"', '').toUpperCase()
        item.section1 = item.section1?.toString(16).replace('"', '').replace('"', '').toUpperCase()
        item.section2 = item.section2?.toString(16).replace('"', '').replace('"', '').toUpperCase()
      })
      this.platforms.value = res?.data
    }
  }

  async add (params: {
    id: string,
    section1: string,
    section2: string,
    sort: number,
    station: string,
    direction: number,
    // turnout: string,
    // stop: number,
    // signal: number
  }) {
    const res = await this.netService.post('/platforms/insert', params)
    if (res?.success) {
      params.id = parseInt(params.id).toString(16).toUpperCase()
      params.sort = Number(params.sort)
      params.section1 = parseInt(params.section1).toString(16).toUpperCase()
      params.section2 = parseInt(params.section2).toString(16).toUpperCase()
      params.station = parseInt(params.station).toString(16).toUpperCase()
      this.platforms.value.push(params)
    }
    return res
  }

  async change (params: {
    id: string,
    section1: string,
    section2: string,
    station: string,
    direction: number,
    sort: number,
    // turnout: string,
    // stop: number,
    // signal: number
  }) {
    const res = await this.netService.post('/platforms/update', params)
    if (res?.success) {
      params.id = parseInt(params.id).toString(16).toUpperCase()
      params.sort = Number(params.sort)
      params.section1 = parseInt(params.section1).toString(16).toUpperCase()
      params.section2 = parseInt(params.section2).toString(16).toUpperCase()
      params.station = parseInt(params.station).toString(16).toUpperCase()

      const delIndex = this.platforms.value.findIndex((e, index) => {
        return e.id === params.id
      })
      this.platforms.value.splice(delIndex, 1, params)
    }
    return res
  }

  async del (params: { id: string }) {
    const res = await this.netService.post('/platforms/delete', params, 'posturl')
    if (res?.success) {
      const delIndex = this.platforms.value.findIndex((e, index) => {
        return e.id === parseInt(params.id).toString(16).toUpperCase()
      })
      this.platforms.value.splice(delIndex, 1)
    }
    return res
  }
}
